package com.own.business.system.user.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.business.system.user.business.service.SystemUserService;
import com.own.business.system.user.common.constant.ConstantModule;
import com.own.business.system.user.entity.dto.SystemUserDto;
import com.own.business.system.user.entity.map.SystemUserMapVo;
import com.own.business.system.user.entity.query.SystemUserQuery;
import com.own.business.system.user.entity.vo.SystemUserVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ManageSystemUserController
 *
 * @author chenxueli
 * @date 2023-08-09 15:58:48
 */
@Tag(name = "系统用户" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@PermissionModule(ConstantModule.USER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "system/user")
public class ManageSystemUserController extends BaseController {

    @Resource
    private SystemUserService systemUserService;

    @LogOperationAop
    @Operation(summary = "分页查询信息")
    @GetMapping("/page")
    public ResultModel<PageModel<SystemUserVo>> page(
            @RequestBody SystemUserQuery query
    ) {
        var pageModel = systemUserService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @Operation(summary = "map查询信息")
    @GetMapping("/map")
    public ResultModel<List<SystemUserMapVo>> map(
            @RequestBody SystemUserQuery query
    ) {
        var list = systemUserService.map(query);
        return R.success(list);
    }

    @LogOperationAop
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<SystemUserVo> add(
            @Validated @RequestBody SystemUserDto dto
    ) {
        dto.check();
        var updateModel = systemUserService.add(dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @Operation(summary = "修改信息")
    @PutMapping("/{systemUserId}")
    public ResultModel<SystemUserVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemUserId,
            @Validated @RequestBody SystemUserDto dto
    ) {
        dto.check();
        var updateModel = systemUserService.update(systemUserId, dto.toBo());
        return R.success(updateModel);
    }

    @LogOperationAop
    @ConfirmAop(message = "确认删除该项目？（注意：删除之后无法找回数据，且该项目所有资料会被删除）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{systemUserId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemUserId
    ) {
        var updateModel = systemUserService.delete(systemUserId);
        return R.success(updateModel);
    }

    @LogOperationAop
    @Operation(summary = "根据id获取信息")
    @GetMapping("/{systemUserId}")
    public ResultModel<SystemUserVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long systemUserId
    ) {
        var vo = systemUserService.getById(systemUserId);
        return R.success(vo);
    }

}
